package queryProccessor;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class Delete extends Command {
	String selectStatment;
	ArrayList<String> finalArray;
	String condition;
	private final String[] ideal1 = { "DELETE FROM", "table_name" };
	private final String[] ideal2 = { "DELETE FROM", "table_name", "WHERE",
			"condition" };
	boolean result = true;
	
	

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public Delete(String s) {
		selectStatment = s;
		finalArray = new ArrayList<String>();
		convertToArray();
		result = checkSyntax();
	}

	@Override
	public void convertToArray() {
		int del = search("DELETE FROM ", selectStatment);
		int wh = search("WHERE ", selectStatment);
		int sc = search(";", selectStatment);

		if (del != -1 && sc != -1) {
			finalArray.add("DELETE FROM");
			String s = "";
			if (wh != -1) {
				s = selectStatment.substring(del + 11, wh);
				s = s.trim();
				finalArray.add(s);
				finalArray.add("WHERE");
				s = selectStatment.substring(wh + 5, sc);
				s = s.trim();
				finalArray.add(s);

			} else {
				s = selectStatment.substring(del + 11, sc);
				s = s.trim();
				finalArray.add(s);
			}

		}

	}

	public boolean lools(String[] ideal, int[] no) {
		boolean result = true;
		for (int i = 0; i < finalArray.size(); i++) {
			if (!isTrue(i, no)) {
				if (!finalArray.get(i).equalsIgnoreCase(ideal[i])) {
					result = false;
					break;
				}
			}
		}
		return result;
	}

	private boolean isTrue(int x, int[] no) {
		for (int i = 0; i < no.length; i++) {
			if (no[i] == x)
				return true;
		}
		return false;
	}

	public boolean checkSyntax() {
		boolean result = true;
		if (finalArray.size() == 2) {
			int[] no = { 1 };
			if (result == lools(ideal1, no)) {
				table_name = finalArray.get(1);
			}
		} else if (finalArray.size() == 4) {
			int[] no = { 1, 3 };
			if (result == lools(ideal2, no)) {
				table_name = finalArray.get(1);
				condition = finalArray.get(3);
			}
		} else
			result = false;
		return result;
	}

	
}
